package com.company.cloud.crm.modules.mgr.service.impl;

import java.time.LocalDateTime;

import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.crm.constant.CrmBizConstant.AllocationStateEnum;
import com.company.cloud.crm.modules.base.entity.CustomerAllocationLog;
import com.company.cloud.crm.modules.base.entity.CustomerContactInfo;
import com.company.cloud.crm.modules.base.entity.CustomerInfo;
import com.company.cloud.crm.modules.base.service.CustomerAllocationLogService;
import com.company.cloud.crm.modules.base.service.CustomerContactInfoService;
import com.company.cloud.crm.modules.base.service.CustomerInfoService;
import com.company.cloud.crm.modules.mgr.service.CustomerContactMgrService;
import com.company.cloud.crm.utils.CrmBizId;
import com.company.cloud.user.modules.api.response.UserFull;
import com.company.cloud.user.modules.api.service.UserRemoteService;

@Service
public class CustomerContactMgrServiceImpl implements CustomerContactMgrService{

	
	@Autowired
	private CustomerContactInfoService customerContactInfoService;
	@Autowired
	private CustomerInfoService customerInfoService;
	@Autowired
	private UserRemoteService userRemoteService;
	@Autowired
	private CustomerAllocationLogService customerAllocationLogService;

	@Transactional
	@Override
	public CustomerContactInfo addCustomerContact(Integer custUserId, Integer userId,String ip) {
		if(custUserId==userId){
			return null;
		}
		CustomerContactInfo contact1=customerContactInfoService.getOne(new QueryWrapper<CustomerContactInfo>()
				.eq("contact_user_id", userId)
				.eq("user_id", custUserId)
				);
		if(contact1!=null) {
			return contact1;
		}
		
		CustomerInfo customer= this.getCustomerByUserId(custUserId,userId,ip);
		CustomerContactInfo contact=customerContactInfoService.getOne(new QueryWrapper<CustomerContactInfo>()
				.eq("contact_user_id", userId)
				.eq("cust_no", customer.getCustNo())
				);
		
		if(contact!=null) {
			return contact;
		}
		int count= customerContactInfoService.count(new QueryWrapper<CustomerContactInfo>()
				.eq("cust_no", customer.getCustNo()));
		contact=new CustomerContactInfo();
		contact.setUserId(customer.getUserId());
		contact.setCustNo(customer.getCustNo());
		contact.setContactUserId(userId);
		if(count>0) {
			contact.setIsExclusiveContact(YNEnum.NO.getCode());
		}else {
			contact.setIsExclusiveContact(YNEnum.YES.getCode());
		}
		contact.setCman(userId);
		contact.setCdt(LocalDateTime.now());
		contact.setCip(ip);
		contact.setEman(userId);
		contact.setEdt(LocalDateTime.now());
		contact.setEip(ip);
		customerContactInfoService.save(contact);
		return contact;
	}
	
	/***/
	public CustomerInfo getCustomerByUserId(Integer custUserId,Integer userId,String ip) {
		CustomerInfo entity=customerInfoService.getOne(new QueryWrapper<CustomerInfo>()
				.eq("user_id", custUserId));
		if(entity!=null) {
			return entity;
		}
		BaseClientResult<UserFull> userResp=userRemoteService.getFullUserById(custUserId);
		
		entity=new CustomerInfo();
		entity.setCustNo(CrmBizId.getCustomerNo());
		if (BaseReturnEnum.SUCCESS.getCode().equals(userResp.getCode()) && null != userResp.getData()) {
			UserFull user = userResp.getData();
			entity.setCompanyNo(user.getCompanyNo());
			entity.setUserId(user.getId());
			if(StringUtils.hasText(user.getRealName())) {
				entity.setName(user.getRealName());
			}else if(StringUtils.hasText(user.getNickName())) {
				entity.setName(user.getNickName());
			}else {
				entity.setName(user.getUserName());
			}
			entity.setTel(user.getTel());
			entity.setMobile(user.getMobile());
			entity.setCman(user.getId());
			entity.setCip(user.getRegIp());
			entity.setEman(user.getId());
			entity.setEip(user.getRegIp());
		}
//		entity.setGrade(params.getGrade());
//		entity.setSourceOne(params.getSourceOne());
//		entity.setSourceTwo(params.getSourceTwo());
//		entity.setIndustryOne(params.getIndustryOne());
//		entity.setIndustryTwo(params.getIndustryTwo());
//		entity.setMail(params.getMail());
//		entity.setFax(params.getFax());
//		entity.setWebsiteUrl(params.getWebsiteUrl());
//		entity.setRemark(params.getRemark());
//		entity.setNationCode(params.getNationCode());
//		entity.setProvince(params.getProvince());
//		entity.setCity(params.getCity());
//		entity.setRegion(params.getRegion());
//		entity.setStreet(params.getStreet());
//		entity.setAddr(params.getAddr());
//		entity.setLongitude(params.getLongitude());
//		entity.setLatitude(params.getLatitude());
		entity.setSaleUserId(userId);
		entity.setAllocationState(AllocationStateEnum.Yes_Allocation.getCode());
		entity.setCdt(LocalDateTime.now());
		entity.setEdt(LocalDateTime.now());
		entity.setRowsState(BaseConstant.EnabledEnum.ENABLED.getCode());
		customerInfoService.save(entity);
		
		CustomerAllocationLog log=new CustomerAllocationLog();
		log.setId(CrmBizId.getCustomerAllocationLogId());
		log.setCompanyNo(entity.getCompanyNo());
		log.setCustNo(entity.getCustNo());
		log.setAllocationUserId(entity.getSaleUserId());
		log.setStartDate(LocalDateTime.now());
		log.setAllocationOpUserId(userId);
		log.setAllocationOpDate(LocalDateTime.now());
		log.setAllocationOpIp(ip);
		log.setCdt(LocalDateTime.now());
		log.setCman(userId);
		log.setCip(ip);
		customerAllocationLogService.save(log);
		
		return entity;
	}
	
	
	
	
	
}
